﻿using MslDiary.Model;
using MslDiary.Utils;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows.Input;

// 版权所有：2014  All Rights Reserved.
// 文 件 名：ItemListViewModel.cs
// 功能描述：
// 创建标识：m.sh.lin0328@163.com 2014/12/13 22:37:03
// 修改描述：
//----------------------------------------------------------------*/
namespace MslDiary.ViewModel
{
    public class ItemListViewModel : MslDiary.Framework.ViewModelBase
    {
        private MslDiary.BLL.A_ItemBLL itemBLL = new MslDiary.BLL.A_ItemBLL();
        private int parentId = 0;
        public ItemListViewModel()
        {
            this.WindowLoadCommand = new CommandBase((Object p) =>
            {
                LoadItemList();
            });

            this.UpdateCommand = new CommandBase((Object parameter) => Update());
            this.DeleteCommand = new CommandBase((Object m) => Delete());
            this.ResetCommand = new CommandBase((Object parameter) =>
            {
                if (this.ItemMod == null)
                {
                   MslDiary.Utils.Helper.Instance.AlertWarning("您还没有选中节点！");
                    return;
                }
                this.parentId = this.ItemMod.Id;
                this.FatherId = this.ItemMod.FatherId;
                this.ItemMod = new Model.A_Item();
                this.ItemMod.IsDelete = "0";
                this.ItemMod.IsEnable = "1";
                this.ButtonAcceptText = "添加";

                this.ShowAddButton = true;
                this.ShowDeleteButton = false;
                this.ShowResetButton = false;
                this.ShowUpdateButton = false;
            });
            this.SelectedItemChangedCommand = new CommandBase((Object m) =>
            {
                this.ShowAddButton = false;
                this.ShowDeleteButton = true;
                this.ShowResetButton = true;
                this.ShowUpdateButton = true;

                this.SelectedTreeNode = (TreeNodeViewModel)m;
                if (this.SelectedTreeNode != null)
                {
                    this.FatherId = this.SelectedTreeNode.ParentID;
                    this.ItemMod = itemBLL.GetEntity(" Id=" + this.SelectedTreeNode.ID);
                    this.IsEnable = (this.ItemMod.IsEnable == null || this.ItemMod.IsEnable.Equals("1")) ? true : false;
                    this.ButtonAcceptText = "更新";
                }
            });
            this.AddItemCommand = new CommandBase((Object m) =>
            {
                this.ButtonAcceptText = "添加";
                this.ItemMod.IsEnable = this.IsEnable ? "1" : "0";
                Add(m.ToString());
            });
        }

        #region 属性
        private Model.A_Item _itemMod;
        private string _buttonAcceptText = "添加";
        private List<TreeNodeViewModel> _treeNodeList;
        private bool _showAddButton = false;
        private bool _showResetButton = true;
        private bool _showDeleteButton = false;
        private bool _showUpdateButton = true;
        private TreeNodeViewModel _selectedTreeNode;
        private bool _isEnable = true;
        private int _fatherId = 0;

        /// <summary> 是否可用 </summary>
        public bool IsEnable
        {
            get { return _isEnable; }
            set
            {
                //base.SetProperty<bool>(ref this._isEnable, value);
                if (this._isEnable == value)
                    return;
                this._isEnable = value;
                this.RaisePropertyChanged("IsEnable");
            }
        }
        /// <summary> 是否显示删除按钮 </summary>
        public bool ShowDeleteButton
        {
            get { return _showDeleteButton; }
            set
            {
                //base.SetProperty<bool>(ref this._showDeleteButton, value);
                if (this._showDeleteButton == value)
                    return;
                this._showDeleteButton = value;
                this.RaisePropertyChanged("ShowDeleteButton");
            }
        }
        /// <summary> 是否显示添加按钮 </summary>
        public bool ShowAddButton
        {
            get { return _showAddButton; }
            set
            {
                //base.SetProperty<bool>(ref this._showAddButton, value);
                if (this._showAddButton == value)
                    return;
                this._showAddButton = value;
                this.RaisePropertyChanged("ShowAddButton");
            }
        }
        /// <summary> 是否显示添加按钮 </summary> 
        public bool ShowResetButton
        {
            get { return _showResetButton; }
            set
            {
                //base.SetProperty<bool>(ref this._showResetButton, value);
                if (this._showResetButton == value)
                    return;
                this._showResetButton = value;
                this.RaisePropertyChanged("ShowResetButton");
            }
        }
        /// <summary> 是否显示更新按钮 </summary> 
        public bool ShowUpdateButton
        {
            get { return _showUpdateButton; }
            set
            {
                //base.SetProperty<bool>(ref this._showUpdateButton, value);
                if (this._showUpdateButton == value)
                    return;
                this._showUpdateButton = value;
                this.RaisePropertyChanged("ShowUpdateButton");
            }
        }
        /// <summary> 父编码 </summary>
        public int FatherId
        {
            get { return _fatherId; }
            set
            {
                //base.SetProperty<int>(ref this._fatherId, value);
                if (this._fatherId == value)
                    return;
                this._fatherId = value;
                this.RaisePropertyChanged("FatherId");
            }
        }
        public A_Item ItemMod
        {
            get { return _itemMod; }
            set
            {
                //base.SetProperty<A_Item>(ref this._itemMod, value);
                if (this._itemMod == value)
                    return;
                this._itemMod = value;
                this.RaisePropertyChanged("ItemMod");
            }
        }
        /// <summary> 保存文字 </summary>
        public string ButtonAcceptText
        {
            get { return _buttonAcceptText; }
            set
            {
                //base.SetProperty<string>(ref this._buttonAcceptText, value);
                if (this._buttonAcceptText == value)
                    return;
                this._buttonAcceptText = value;
                this.RaisePropertyChanged("ButtonAcceptText");
            }
        }
        /// <summary>树节点</summary> 
        public List<TreeNodeViewModel> TreeNodeList
        {
            get { return _treeNodeList; }
            set
            {
                //base.SetProperty<List<TreeNodeViewModel>>(ref this._treeNodeList, value);
                if (this._treeNodeList == value)
                    return;
                this._treeNodeList = value;
                this.RaisePropertyChanged("TreeNodeList");
            }
        }
        /// <summary> 被选中的节点 </summary>
        public TreeNodeViewModel SelectedTreeNode
        {
            get { return _selectedTreeNode; }
            set
            {
                //base.SetProperty<TreeNodeViewModel>(ref this._selectedTreeNode, value);
                if (this._selectedTreeNode == value)
                    return;
                this._selectedTreeNode = value;
                this.RaisePropertyChanged("SelectedTreeNode");
            }
        }
        #endregion

        #region 命令

        /// <summary> 保存</summary>
        public ICommand UpdateCommand { get; set; }
        /// <summary> 保存</summary>
        public ICommand ResetCommand { get; set; }
        /// <summary> 删除</summary>
        public ICommand DeleteCommand { get; set; }

        /// <summary> 选中树</summary>
        public ICommand SelectedItemChangedCommand { get; set; }
        /// <summary> 添加项 </summary>
        public ICommand AddItemCommand { get; set; }
        public ICommand WindowLoadCommand { get; set; }

        #endregion

        #region 方法
        /// <summary>
        /// 加载字典列表
        /// </summary>
        private void LoadItemList()
        {
            List<Model.A_Item> list = itemBLL.GetList(" IsDelete='0' ", " Id,Name,FatherId ", " FatherId ASC");

            ArrayList al = TreeHelper.GetExpandedId(TreeNodeList);
            int selectedId = 0;//当前选中项Id
            if (this.SelectedTreeNode != null)
                selectedId = this.SelectedTreeNode.ID;

            if (list != null && list.Count > 0)
            {
                List<TreeNodeViewModel> nodes = new List<TreeNodeViewModel>();
                foreach (Model.A_Item mod in list)
                {
                    nodes.Add(new TreeNodeViewModel
                    {
                        ID = mod.Id,
                        DisplayName = mod.Name,
                        Icon = "/Theme/Images/item.png",
                        ParentID = mod.FatherId,
                        IsExpanded = (al != null && al.Contains(mod.Id) ? true : false),
                        IsSelected = (selectedId != 0 && selectedId == mod.Id ? true : false)
                    });
                }
                TreeNodeList = TreeHelper.Bind(nodes);
            }
        }
        private void Delete()
        {
            if (this.ItemMod == null)
            {
               MslDiary.Utils.Helper.Instance.AlertWarning("没有选中节点！");
                return; ;
            }
            if (this.ItemMod.IsAllowDelete != null && this.ItemMod.IsAllowDelete.Equals("0"))
            {
               MslDiary.Utils.Helper.Instance.AlertSuccess("此节点[" + this.ItemMod.Name + "]不允许删除！");
                return; ;
            }
            string msg = String.Empty;
           MslDiary.Utils.Helper.Instance.AlertConfirm("警告",MslDiary.Framework.ShowMessage.DEL_MSG02, () =>
            {
                int num = itemBLL.DeleteById(this.ItemMod.Id, out msg);
                if (num > 0)
                {
                    LoadItemList();
                   MslDiary.Utils.Helper.Instance.AlertSuccess("节点[" + this.ItemMod.Name + "]删除成功！");
                }
            });
            if (msg != null && msg.Length > 0)
               MslDiary.Utils.Helper.Instance.AlertWarning(msg);
        }
        private void Update()
        {
            if (this.ItemMod == null)
            {
               MslDiary.Utils.Helper.Instance.AlertWarning("没有选中节点！");
                return;
            }
            if (String.IsNullOrEmpty(ItemMod.Name))
            {
                Utils.Helper.Instance.AlertWarning("名称不能为空！");
                return;
            }
            try
            {
                ItemMod.ModifyDate = DateTime.Now;
                this.ItemMod.IsEnable = this.IsEnable ? "1" : "0";
                if (itemBLL.Update(ItemMod, " Id=" + ItemMod.Id) > 0)
                {
                   MslDiary.Utils.Helper.Instance.AlertSuccess(MslDiary.Framework.ShowMessage.OPERATE_MSG01);
                    LoadItemList();
                    return;
                }
               MslDiary.Utils.Helper.Instance.AlertSuccess(MslDiary.Framework.ShowMessage.OPERATE_MSG02);
            }
            catch (Exception ex)
            {
                MSL.Tool.LogHelper.Instance.WriteLog("ItemListViewModel Update\r\n" + ex.ToString());
               MslDiary.Utils.Helper.Instance.AlertError(MslDiary.Framework.ShowMessage.SYS_MSG01);
            }
        }
        private void Add(string type)
        {
            if (ItemMod == null || String.IsNullOrEmpty(ItemMod.Name))
            {
                Utils.Helper.Instance.AlertWarning("名称不能为空！");
                return;
            }
            if (type.Equals("1"))//添加根项
            {
                if (this.ItemMod.FatherId != 0)
                {
                    Utils.Helper.Instance.AlertWarning("先重置后再添加！");
                    return;
                }
                this.ItemMod.FatherId = 0;
            }
            else//添加子项
            {
                this.ItemMod.FatherId = this.parentId;
            }
            try
            {
                this.ItemMod.IsAllowDelete = "1";
                this.ItemMod.IsDelete = "0";
                if (itemBLL.Add(ItemMod) > 0)
                {
                   MslDiary.Utils.Helper.Instance.AlertSuccess(MslDiary.Framework.ShowMessage.OPERATE_MSG01);
                    LoadItemList();
                    this.ShowAddButton = false;
                    return;
                }
               MslDiary.Utils.Helper.Instance.AlertSuccess(MslDiary.Framework.ShowMessage.OPERATE_MSG02);
            }
            catch (Exception ex)
            {
                MSL.Tool.LogHelper.Instance.WriteLog("ItemListViewModel Add\r\n" + ex.ToString());
               MslDiary.Utils.Helper.Instance.AlertError(MslDiary.Framework.ShowMessage.SYS_MSG01);
            }
        }
        #endregion
    }
}
